package com.example.museum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.museum.common.Result;
import com.example.museum.entity.CulturalInfo;
import com.example.museum.service.CulturalInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cultural-info")
public class CulturalInfoController {

    @Autowired
    private CulturalInfoService culturalInfoService;

    /**
     * 分页获取文化资讯列表
     */
    @GetMapping("/pagelist")
    public Result<Page<CulturalInfo>> getPageList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String query) {
        
        Page<CulturalInfo> pageResult = culturalInfoService.getPageList(page, pageSize, query);
        return Result.success(pageResult);
    }

    /**
     * 添加文化资讯
     */
    @PostMapping("/add")
    public Result<Boolean> addCulturalInfo(@RequestBody CulturalInfo culturalInfo) {
        boolean success = culturalInfoService.addCulturalInfo(culturalInfo);
        return success ? Result.success(true) : Result.failed("添加文化资讯失败");
    }

    /**
     * 获取文化资讯详情
     */
    @GetMapping("/detail/{id}")
    public Result<CulturalInfo> getDetail(@PathVariable Long id) {
        CulturalInfo info = culturalInfoService.getInfoById(id);
        return info != null ? Result.success(info) : Result.failed("文化资讯不存在");
    }

    /**
     * 更新文化资讯
     */
    @PutMapping("/update")
    public Result<Boolean> updateCulturalInfo(@RequestBody CulturalInfo culturalInfo) {
        boolean success = culturalInfoService.updateCulturalInfo(culturalInfo);
        return success ? Result.success(true) : Result.failed("更新文化资讯失败");
    }

    /**
     * 删除文化资讯
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteCulturalInfo(@PathVariable Long id) {
        boolean success = culturalInfoService.deleteCulturalInfo(id);
        return success ? Result.success(true) : Result.failed("删除文化资讯失败");
    }
}